<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <h2 class="login-title">后台管理系统登录</h2>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="80px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" :loading="loading" @click="handleLogin">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'
  import type { FormInstance, FormRules } from 'element-plus'

  const router = useRouter()
  const loginFormRef = ref<FormInstance>()
  const loading = ref(false)

  // 登录表单数据
  const loginForm = ref({
    username: '',
    password: ''
  })

  // 表单验证规则
  const loginRules = ref<FormRules>({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
    ]
  })

  // 处理登录
  const handleLogin = async () => {
    try {
      // 验证表单
      await loginFormRef.value?.validate()

      loading.value = true

      // 模拟登录请求
      setTimeout(() => {
        // 登录成功后设置token
        const mockToken = 'mock-jwt-token'
        localStorage.setItem('token', mockToken)
        loading.value = false
        // 跳转到首页
        router.push('/')
      }, 1000)
    } catch (error) {
      console.error('登录失败:', error)
      loading.value = false
    }
  }
</script>

<style scoped lang="less">
  .login-container {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    height: 100vh;
    background-image: url('@/assets/images/background.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    padding-right: 100px;
  }

  .login-form-wrapper {
    background-color: rgba(255, 255, 255, 0.9);
    position: absolute;
    top: 35%;
    right: 6%;
    transform: translateY(-50%);
    padding: 48px;
    box-sizing: border-box;
    width: 460px;
    height: 550px;
    box-shadow: 0 0 16px #2e31361a;
    border-radius: 8px;
    :deep(.el-form-item) {
      .el-form-item__content {
        .el-input {
          height: 48px;
          border-radius: 4px;
          .el-input__wrapper {
            background: #f3f4f9;
          }
        }
      }
    }
  }

  .login-title {
    text-align: center;
    margin-bottom: 30px;
    color: #304156;
  }

  .login-form {
    .login-btn {
      width: 100%;
    }
  }
</style>
